package com.skytech.tax.customsdatawarehouse.singlewindow.customsagententrusted.domain.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.skytech.tax.customsdatawarehouse.common.util.SecurityUtil;
import com.skytech.tax.customsdatawarehouse.singlewindow.customsagententrusted.domain.dao.EntrustedAgreementDao;
import com.skytech.tax.customsdatawarehouse.singlewindow.customsagententrusted.domain.dao.EntrustedAgreementFileDao;
import com.skytech.tax.customsdatawarehouse.singlewindow.customsagententrusted.domain.entity.EntrustedAgreement;
import com.skytech.tax.customsdatawarehouse.singlewindow.customsagententrusted.domain.entity.EntrustedAgreementFile;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Lianhong
 * @since 2022/6/15
 */
@Service
public class EntrustedAgreementService {

    private static final Logger LOGGER = LoggerFactory.getLogger(EntrustedAgreementService.class);

    @Value("${customsdatawarehouse.token}")
    private String token;

    @Resource
    private EntrustedAgreementDao entrustedAgreementDao;
    @Resource
    private EntrustedAgreementFileDao entrustedAgreementFileDao;

    public void saveEntrustedAgreement(String entrustedAgreement) {
        if (StringUtils.isEmpty(entrustedAgreement)) return;
        String entrustedAgreementText = SecurityUtil.decrypt3DES(token, entrustedAgreement);
        try {
            JSONObject entrustedAgreementObj = JSON.parseObject(entrustedAgreementText);
            if (entrustedAgreementObj == null) return;
            String socialCreditCode = entrustedAgreementObj.getString("socialCreditCode");
            JSONArray entrustedAgreementArr = entrustedAgreementObj.getJSONArray("entrustedAgreements");
            if (CollectionUtils.isEmpty(entrustedAgreementArr)) return;

            //委托协议列表拆分并存储
            List<EntrustedAgreement> entrustedAgreementList = new ArrayList<>();
            for (int i = 0; i < entrustedAgreementArr.size(); i++) {
                JSONObject item = entrustedAgreementArr.getJSONObject(i);
                EntrustedAgreement agreement = new EntrustedAgreement(socialCreditCode, item);
                entrustedAgreementList.add(agreement);
            }
            if (CollectionUtils.isEmpty(entrustedAgreementList)) return;

            this.deleteBySeqNo(entrustedAgreementList.stream().map(EntrustedAgreement::getSeqNo).collect(Collectors.toList()));

            entrustedAgreementDao.save(entrustedAgreementList);
        } catch (Exception e) {
            LOGGER.error("保存委托协议错误", e);
        }

    }

    private void deleteBySeqNo(List<String> seqNos) {
        entrustedAgreementDao.deleteBy(seqNos);
    }

    public String entrustedAgreementFile(String seqNo) {
        return entrustedAgreementFileDao.findBySeqNo(seqNo);
    }

    public void saveCustomsDeclarationFile(String seqNo, String storedFile) {
        EntrustedAgreementFile entrustedAgreementFile = new EntrustedAgreementFile(seqNo, storedFile);
        entrustedAgreementFileDao.save(entrustedAgreementFile);
    }

    public boolean entrustedAgreementFileStored(String seqNo) {
        return entrustedAgreementFileDao.exists(seqNo);
    }
}
